package models

import (
	"crypto/md5"
	"fmt"
	"io"
	"mime/multipart"
	"os"
	"path"
)

func Md5Password(str string) string {
	data := []byte(str)
	has := md5.Sum(data)
	md5str1 := fmt.Sprintf("%x", has) //将[]byte转成16进制

	fmt.Println(md5str1)
	return md5str1
}
func GetInage(file multipart.File, m *multipart.FileHeader, err error) (string, error) {
	if err != nil {
		return "", nil
	}
	if m.Size > 2<<20 {
		err1 := fmt.Errorf("图片过大")
		return "", err1
	}
	if path.Ext(m.Filename) != ".jpg" && path.Ext(m.Filename) != ".png" && path.Ext(m.Filename) != ".jpeg" {
		err2 := fmt.Errorf("格式错误")
		return "", err2
	}
	openFile, err := os.OpenFile("./static/img/"+m.Filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 777)
	if err != nil {
		return "", err
	}
	io.Copy(openFile, file)
	return "http://14.103.158.217:8080/static/img/" + m.Filename, nil
}
